<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- 添加按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="handleaddboxsort"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <!-- 添加表格   -->
      <treeTable
        :data="catelist"
        :columns="columns"
        :expand-type="false"
        :selection-type="false"
        show-index
        index-text="#"
        :show-row-hover="false"
        border
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-circle-check"
            v-if="scope.row.cat_deleted == false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-circle-close" style="color: lightgreen" v-else></i>
        </template>
        <!-- 排序吗 -->
        <template slot="order" slot-scope="scope">
        
          <el-tag size="mini" v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag
            size="mini"
            type="success"
            v-else-if="scope.row.cat_level == 1"
            >二级</el-tag
          >
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="ptn">
          <el-button type="primary" size="mini" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button type="danger" size="mini" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </treeTable>
    </el-card>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="getqueryinfo.pagenum"
      :page-sizes="[3, 5, 10, 15]"
      :page-size="getqueryinfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 添加弹框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addboxsort"
      width="50%"
      @close="addboxclose"
    >
      <!-- 表单 -->
      <el-form
        :model="addruleForm"
        :rules="addrules"
        ref="addref"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addruleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!-- 配置对象props -->
          <el-cascader
            v-model="addselect"
            :options="addlist"
            :props="addprops"
            expandTrigger="hover"
            @change="handleChange"
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addboxsort = false">取 消</el-button>
        <el-button type="primary" @click="addsandquery">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
export default {
  data() {
    return {
      // 定义列表数据参数
      getqueryinfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      // 获取列表数据
      catelist: [],
      // 获取列表总数
      total: 0,

      //   列表分类
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "isok",
        },
        {
          label: "排序",
          prop: "cat_level",
          type: "template",
          template: "order",
        },
        {
          label: "操作",

          type: "template",
          template: "ptn",
        },
      ],
      //   弹框boolean
      addboxsort: false,
      //   表单数据
      addruleForm: {
        cat_name: "",
        // 分类父 ID
        cat_pid: 0,
        // 分类层级
        cat_level: 0,
      },
      //   验证
      addrules: {
        cat_name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
      //   添加列表数据
      addlist: [],
      // props配置对象
      addprops: {
        expandTrigger: 'hover',
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      //   级联选择器数据
      addselect: [],
    };
  },
  created() {
    this.getquery();
  },
  methods: {
    async getquery() {
      const { data: res } = await this.$http.get("categories", {
        params: this.getqueryinfo,
      });
      //   console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取列表失败");
      }
      this.catelist = res.data.result;
      this.total = res.data.total;
    },
    // 每页的大小
    handleSizeChange(sizes) {
      this.getqueryinfo.pagesize = sizes;
      this.getquery();
    },
    // 当前第几页
    handleCurrentChange(num) {
      this.getqueryinfo.pagenum = num;
      this.getquery();
    },
    // 添加事件
    handleaddboxsort() {
      this.addqueryselectlist();
      this.addboxsort = true;
    },
    // 获取级联选择列表
    async addqueryselectlist() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 },
      });
      console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取列表失败");
      }
      this.addlist = res.data;
    },
    handleChange() {
      if (this.addselect.length > 0) {
        this.addruleForm.cat_pid = this.addselect[this.addselect.length - 1];
        this.addruleForm.cat_level = this.addselect.length;
        return;
      } else {
        this.addruleForm.cat_pid = 0;
        this.addruleForm.cat_level = 0;
      }
    },
    // 发送请求  与校验
    addsandquery() {
      this.$refs.addref.validate(async (valid) => {
        if (!valid) {
          return this.$message.error("请输入活动名称");
        }
        const { data: res } = await this.$http.post(
          "categories",
          this.addruleForm
        );
        console.log(res, "..");
        if (res.meta.status !== 201) {
          return this.$message.error("添加失败");
        }
        this.$message.success("添加成功");
        this.getquery();
        this.addboxsort = false;
      });
    },
    addboxclose() {
      this.$refs.addref.resetFields();
      this.addruleForm.cat_pid = 0;
      this.addruleForm.cat_level = 0;
      this.addselect = [];
    },
  },
};
</script>

<style lang="less" scoped>
.el-row {
  margin-bottom: 10px;
}
.el-cascader {
  width: 100%;
}
</style>